$(document).ready(function () {
    updateUserCombo();
    getAuthors();

    $("#btnAddAuthor").click(function () {
        var strArtId = $("#articleId").val();
        var strUsrId = $("#UserList").val();
        $.ajax(
        {
            type: "GET",
            contentType: "application/json; charset=iso8859-1",
            url: "/Article/WSAddAuthors/",
            data: { "articleId": strArtId, "userId": strUsrId },
            dataType: "json",
            success: function (data) {
                updateUserCombo();
                getAuthors();
            },
            error: function (xhr, err) {
                alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                alert("responseText: " + xhr.responseText);
            }
        });
    });

});



function updateUserCombo()
{
    var strArtId = $("#articleId").val();
    $.ajax(
    {
        type: "GET",
        contentType: "application/json; charset=iso8859-1",
        url: "/Article/WSGetExceptUsers/",
        data: { "articleId": strArtId },
        dataType: "json",
        success: function (data) {
            $("#UserList").empty();
            $("#UserListTemplate").tmpl(data).hide().appendTo("#UserList").fadeIn("slow");
        },
        error: function (xhr, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        }
    });
};

function removeAuthor(strUsrId) {
    var strArtId = $("#articleId").val();
    $.ajax(
    {
        type: "GET",
        contentType: "application/json; charset=iso8859-1",
        url: "/Article/WSRemoveAuthor/",
        data: { "articleId": strArtId, "userId": strUsrId },
        dataType: "json",
        success: function (data) {
            getAuthors();
        },
        error: function (xhr, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        }
    });
};

function getAuthors() {
    var strArtId = $("#articleId").val();
    $.ajax(
    {
        type: "GET",
        contentType: "application/json; charset=iso8859-1",
        url: "/Article/WSGetAuthors/",
        data: { "articleId": strArtId },
        dataType: "json",
        success: function (data) {
            $("#authorsList").empty();
            $("#authorListTemplate").tmpl(data).hide().appendTo("#authorsList").fadeIn("slow");
        },
        error: function (xhr, err) {
            alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        }
    });
};
